package com.smartcruise.util.network;

import com.google.gson.Gson;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.data.cruiseData.NodeTaskListBean;
import com.smartcruise.service.UpLoadLocationManager;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.network.callBack.BaseCallBack;
import com.smartcruise.util.network.customerObserver.SimpleBackObserver;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;

import io.realm.Realm;
import io.realm.RealmList;

/**
 * Created by FengChaoQun
 * on 2018/3/2 0002
 * 任务数据上传(不显示进度框)
 */

public class TaskUploadBack {

    private final int ALL_SIMPLE_UPLOAD = 3;//整体部分上传
    private final int ALL_ALL_UPLOAD = 4;//整体全部上传
    private CruiseData cruiseData;
    private Realm realm;
    private BaseCallBack baseCallBack;
    private int uploadType; //1:单节点部分上传  2:单节点全部上传  3:整体部分上传  4:整体全部上传
    private RealmList<NodeTaskListBean> nodeTaskList;
    private int currentPosition = 0;


    public TaskUploadBack(CruiseData cruiseData, Realm realm) {
        this.cruiseData = cruiseData;
        this.realm = realm;
        nodeTaskList = cruiseData.getNodeTaskList();
    }

    public void uploadSimple(final BaseCallBack baseCallBack) {
        uploadType = ALL_SIMPLE_UPLOAD;
        this.baseCallBack = baseCallBack;
        currentPosition = 0;

        UpLoadLocationManager.getInstance().upLoadLocalData(new BaseCallBack() {//上传任务数据之前先上传本地坐标数据
            @Override
            public void onSuccess(Result result) {
                if (ListUtils.isHaveContent(nodeTaskList)) {
                    uploadNodeOneByOne();
                } else {//如果没有节点任务 直接上传
                    uploadTaskData();
                }
            }

            @Override
            public void onFail(Result result) {
                baseCallBack.onFail(result);
            }

            @Override
            public void onError(Throwable e) {
                baseCallBack.onError(e);
            }
        });
    }

    public void uploadAll(final BaseCallBack baseCallBack) {
        uploadType = ALL_ALL_UPLOAD;
        this.baseCallBack = baseCallBack;
        currentPosition = 0;

        UpLoadLocationManager.getInstance().upLoadLocalData(new BaseCallBack() {//上传任务数据之前先上传本地坐标数据
            @Override
            public void onSuccess(Result result) {
                if (ListUtils.isHaveContent(nodeTaskList)) {
                    uploadNodeOneByOne();
                } else {//如果没有节点任务 直接上传
                    uploadTaskData();
                }
            }

            @Override
            public void onFail(Result result) {
                baseCallBack.onFail(result);
            }

            @Override
            public void onError(Throwable e) {
                baseCallBack.onError(e);
            }
        });
    }

    /**
     * description:循环上传节点数据
     */

    private void uploadNodeOneByOne() {
        if (currentPosition < nodeTaskList.size()) {
            NodeUploadBack nodeUpload = new NodeUploadBack(cruiseData, realm, nodeTaskList.get(currentPosition));
            nodeUpload.setNeedUploadTask(false);
            if (uploadType == ALL_SIMPLE_UPLOAD) {
                nodeUpload.uploadOneNodeSimpleData(new BaseCallBack() {
                    @Override
                    public void onSuccess(Result result) {
                        currentPosition++;
                        uploadNodeOneByOne();
                    }

                    @Override
                    public void onFail(Result result) {
                        baseCallBack.onFail(result);
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseCallBack.onError(e);
                    }
                });
            } else {
                nodeUpload.uploadOneNodeAllData(new BaseCallBack() {
                    @Override
                    public void onSuccess(Result result) {
                        currentPosition++;
                        uploadNodeOneByOne();
                    }

                    @Override
                    public void onFail(Result result) {
                        baseCallBack.onFail(result);
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseCallBack.onError(e);
                    }
                });
            }
        } else {
//            baseCallBack.onSuccess(null);
            uploadTaskData();
        }
    }

    /**
     * description:上传数据包
     */

    private void uploadTaskData() {
        final CruiseData copyDate = realm.copyFromRealm(cruiseData);
        copyDate.backUpId();
        Gson gson = new Gson();
        NetCollection.pushTask2(gson.toJson(copyDate), new SimpleBackObserver(new BaseCallBack() {
            @Override
            public void onSuccess(final Result result) {
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {

                        //上传成功后 更新服务器id
                        String serverId = result.getData();
                        cruiseData.setServerId(serverId);

                        //更新数据上传状态
                        switch (uploadType) {
                            case ALL_SIMPLE_UPLOAD:
                                cruiseData.setUploadStatus(1);
                                break;
                            case ALL_ALL_UPLOAD:
                                cruiseData.setUploadStatus(2);
                                break;
                        }
                        //更新节点数据上传状态
                        if (ListUtils.isHaveContent(nodeTaskList)) {
                            for (NodeTaskListBean nodeTaskListBean : nodeTaskList) {
                                if (uploadType == ALL_SIMPLE_UPLOAD) {
                                    nodeTaskListBean.setUploadStatus(1);
                                } else {
                                    nodeTaskListBean.setUploadStatus(2);
                                }
                                //如果是网络数据 将本地图片清空
                                if (cruiseData.isFromNet()) {
                                    nodeTaskListBean.clearOriginalFiles();
                                }
                            }
                        }
                    }
                });

                if (baseCallBack != null) {
                    baseCallBack.onSuccess(result);
                }
            }

            @Override
            public void onFail(Result result) {
                baseCallBack.onFail(result);
            }

            @Override
            public void onError(Throwable e) {
                baseCallBack.onError(e);
            }
        }));
    }
}
